from django.db import models
from stratplatform.auth.models import User
from stratplatform.game.models import Unit, Building, Map
  
class ContentNode( models.Model ):
  title = models.CharField( max_length=255 )
  text = models.TextField( )
  created_at = models.DateTimeField( auto_now_add=True )
  modified_at = models.DateTimeField( auto_now=True )
  karma = models.IntegerField( default=0 )
  poster = models.ForeignKey( User )
  
  class Meta:
    abstract = True

class Content( ContentNode ):
  published = models.BooleanField( default=0 )
  sticky = models.BooleanField( default=0 )
  slug = models.SlugField( max_length=55 )
  video_link = models.URLField( blank=True )
  replay_file = models.FileField( upload_to='replays', blank=True )
  sc_version = models.CharField( max_length=10 )
  views_count = models.IntegerField( default=0 )
  downloads_count = models.IntegerField( default=0 )
  has_build_order = models.BooleanField( default=0 )
  has_map_instances = models.BooleanField( default=0 )
  has_video = models.BooleanField( default=0 )
  has_replay = models.BooleanField( default=0 )
  
  def __unicode__(self):
    return self.title
  
class Comment( ContentNode ):
  content = models.ForeignKey( Content )
  in_reply_to = models.ForeignKey( 'self', null=True, blank=True )

class UserVote( models.Model ):
  voter = models.ForeignKey( User )
  voted_comment = models.ForeignKey( Comment, blank=True, null=True )
  voted_content = models.ForeignKey( Content, blank=True, null=True )
  created_at = models.DateTimeField( auto_now_add=True )
  is_report = models.BooleanField( default=0 )
  score = models.IntegerField( default=1 )

class Taxonomy( models.Model ):
  TYPE_CHOICES = ( ( 0, 'Category' ), ( 1, 'Tag' ) )

  name = models.CharField( max_length=255 )
  taxonomy_type = models.IntegerField( choices=TYPE_CHOICES ) # 0=Category, 1=Tag
  slug = models.SlugField()
  parent = models.ForeignKey('self', blank=True, null=True)
  content = models.ManyToManyField( Content )
  
class MapInstance( models.Model ):
  game_map = models.ForeignKey( Map )
  content = models.ForeignKey( Content )
  
class GameObjectInstance( models.Model ):
  COLOR_CHOICES = ( (0,'Red'), (1,'Blue'), (2,'Teal') )
  TYPE_CHOICES = ( (0, 'Unit'), (1, 'Building') )

  position_x = models.FloatField()
  position_y = models.FloatField()
  player_color = models.IntegerField( choices=COLOR_CHOICES )
  building = models.ForeignKey( Building, null=True, blank=True )
  unit = models.ForeignKey( Unit, null=True, blank=True )
  map_instance = models.ForeignKey( MapInstance )
  
class BuildOrder( models.Model ):
  content = models.ForeignKey( Content )
  name = models.CharField( max_length=255 )

class BuildOrderElement( models.Model ):
  build_order = models.ForeignKey( BuildOrder )
  building = models.ForeignKey( Building, blank=True, null=True )
  unit = models.ForeignKey( Unit, blank=True, null=True  )
  start_time = models.IntegerField( ) # Start Building Time in Seconds
  # Determines in which line of prodution this element goes. 
  # Barracks build marines and are build by SCVs
  line = models.IntegerField( default=0 )



